﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq;
using SmartMusicBox.DataBase;
using SmartMusicBox.Model;
using System.Linq;
using SmartMusicBox.Logic;
using System.Collections.Generic;

namespace SmartMusicBox.Framework.Helper
{
    public class DBHelper
    {
        readonly private static FMDB db = new FMDB();

        public static void CreateDb()
        {
            if (!db.DatabaseExists())
            {
                db.CreateDatabase();
                AddChannelList(InitLogic.GetAllChannels());
            }
        }

        public static void AddChannel(ChannelModel channel)
        {
            var c = new ChannelTbl();
            c.ChannelNo = channel.ChannelNo;
            c.ChannelName = channel.ChannelName;
            c.IsPinned = channel.IsPinned;
            db.ChannelTable.InsertOnSubmit(c);
            db.SubmitChanges();
        }

        public static void AddChannelList(ChannelModel[] channel)
        {
            var list = from p in channel
                       select new ChannelTbl { 
                           ChannelNo = p.ChannelNo, 
                           ChannelName = p.ChannelName, 
                           IsPinned = p.IsPinned };

            db.ChannelTable.InsertAllOnSubmit(list.ToList());
            db.SubmitChanges();
        }

        public static void ChangePinStauts(ChannelModel channel)
        {
            var c = new ChannelTbl();
            c.ChannelNo = channel.ChannelNo;
            c.IsPinned = channel.IsPinned;
            db.ChannelTable.InsertOnSubmit(c);
            db.SubmitChanges();
        }

        public static IEnumerable<ChannelModel> GetPinnedChannelList()
        {
            var result = from p in db.ChannelTable
                         where p.IsPinned
                         select new ChannelModel { 
                             ChannelNo = p.ChannelNo, 
                             ChannelName = p.ChannelName , 
                             IsPinned = p.IsPinned };

            return result;
        }
    }
}
